<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt>
        <span>河马程序员电商后台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体内容 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="flag?'64px':'200px'">
        <div class="toggle-button" @click="toggle">
          <i :class="flag?'el-icon-s-unfold':'el-icon-s-fold'"></i>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="flag"
          :collapse-transition="false"
          router
          :default-active="sessionIndex"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="(item,index) in meunlist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="icons[index]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="setIndex('/' + subitem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-notebook-2"></i>
                <!-- 文本 -->
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 中间主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      meunlist: '',
      icons: {
        '0': 'iconfont icon-users',
        '1': 'iconfont icon-lock_fill',
        '2': 'iconfont icon-shangpin',
        '3': 'iconfont icon-danju',
        '4': 'iconfont icon-baobiao'
      },
      flag: false,
      sessionIndex: ''
    }
  },
  created () {
    this.getmenulist()
    this.sessionIndex = sessionStorage.getItem('active')
  },
  methods: {
    logout () {
      sessionStorage.clear()
      this.$router.push('/login')
      this.$message({
        showClose: true,
        message: '退出成功！再见~~',
        type: 'info'
      })
    },
    async getmenulist () {
      const { data } = await this.$http.get('menus')
      if (data.meta.status !== 200) {
        return this.$message({
          showClose: true,
          message: data.meta.status.msg,
          type: 'error'
        })
      }
      this.meunlist = data.data
    },
    toggle () {
      this.flag = !this.flag
    },
    setIndex (index) {
      sessionStorage.setItem('active', index)
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
  background-color: #373d41;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}
.el-container {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
  font-size: 18px;
}
.toggle-button {
  width: 100%;
  color: #fff;
  text-align: center;
  cursor: pointer;
  font-size: 14px;
  i {
    font-size: 18px;
  }
}
</style>
